package com.example.neusoft.yuejian_12.Fragment;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.neusoft.yuejian_12.Adapter.AskAdapter;
import com.example.neusoft.yuejian_12.JavaBean.AskList;
import com.example.neusoft.yuejian_12.Listener.MasterQuestionListener;
import com.example.neusoft.yuejian_12.Model.MasterModel;
import com.example.neusoft.yuejian_12.Model.VModel;
import com.example.neusoft.yuejian_12.R;
import com.example.neusoft.yuejian_12.Service.MasterService;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

/**
 * Created by admin on 2017/10/26.
 */

public class AskFragment extends Fragment {
    private Context context;
    private View view;
    private RecyclerView recyc;
    private List<AskList> list;
    private String question;
    private  String userid;
    private Call<List<AskList>> call;
    private String id;
    SharedPreferences sp;
    private EditText comment;
    private Button ask;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view= inflater.inflate(R.layout.fragment_ask, container, false);
        initViews();
        conn();
        initEvents();
        return view;
    }

    private void conn() {
        Retrofit retrofit= VModel.getConn();
        MasterService impls=retrofit.create(MasterService.class);
        call=impls.getAskList(id);
        call.enqueue(new Callback<List<AskList>>() {
            @Override
            public void onResponse(Call<List<AskList>> call, Response<List<AskList>> response) {
                list=response.body();
                AskAdapter a=new AskAdapter(list,context);
                LinearLayoutManager linear=new LinearLayoutManager(context);
                linear.setOrientation(LinearLayoutManager.VERTICAL);
                recyc.setLayoutManager(linear);
                recyc.setItemAnimator(new DefaultItemAnimator());
                recyc.setAdapter(a);
            }

            @Override
            public void onFailure(Call<List<AskList>> call, Throwable t) {
                Toast.makeText(context,"留言列表获取失败",Toast.LENGTH_SHORT).show();
            }
        });

    }

    private void initViews() {
        context=getActivity();
        Intent intent=getActivity().getIntent();
        id=intent.getStringExtra("id");
      /*  sp=context.getSharedPreferences("userInfo",MODE_PRIVATE);
        userid=Integer.valueOf(sp.getString("user_id",null));*/
        userid = getActivity().getSharedPreferences("userInfo", Context.MODE_PRIVATE).getString("user_id", String.valueOf(0));
        Log.v("获取hhh用户id", String.valueOf(userid));
        recyc=(RecyclerView)view.findViewById(R.id.re_ask);
        ask=(Button)view.findViewById(R.id.ask_submit);
        comment=(EditText)view.findViewById(R.id.ask_content);
    }



    public void initEvents() {
        ask.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                question = comment.getText().toString();
                if (question.equals("")) {
                    Toast.makeText(getActivity(), "您输入的信息为空",Toast.LENGTH_SHORT).show();
                } else {
                    MasterModel model=new MasterModel();
                    model.insertComment(id,userid,question,masterQuestionListener);
                    Toast.makeText(getActivity(), "留言发布成功", Toast.LENGTH_SHORT).show();
                    refresh();
                }
            }
        });

    }

    private void refresh() {
        conn();
    }

    MasterQuestionListener masterQuestionListener =new MasterQuestionListener() {
        @Override
        public void onResponse(Integer listBean) {

        }

        @Override
        public void onFail(String msg) {

        }
    };

    }










